import { NextRequest, NextResponse } from 'next/server';
import { UserModel } from '@/models/user';

// GET /api/profile/internships - 获取用户的实习经历
export async function GET(req: NextRequest) {
  try {
    // 从请求头获取token并验证用户身份
    const authHeader = req.headers.get('authorization');
    
    if (!authHeader || !authHeader.startsWith('Bearer ')) {
      return NextResponse.json(
        { message: '未授权访问' },
        { status: 401 }
      );
    }
    
    const token = authHeader.split(' ')[1];
    
    // 解析JWT token
    let userId;
    try {
      const payload = JSON.parse(atob(token.split('.')[1]));
      userId = payload.id;
      
      if (!userId) {
        return NextResponse.json(
          { message: '无效的用户凭证' },
          { status: 401 }
        );
      }
    } catch (e) {
      console.error('解析token失败:', e);
      return NextResponse.json(
        { message: '无效的用户凭证' },
        { status: 401 }
      );
    }
    
    // 获取用户的实习经历
    const internships = await UserModel.getInternships(userId);
    
    return NextResponse.json(
      internships,
      { status: 200 }
    );
  } catch (error: any) {
    console.error('获取实习经历时出错:', error);
    
    return NextResponse.json(
      { message: '获取实习经历失败', error: error.message },
      { status: 500 }
    );
  }
}

// POST /api/profile/internships - 添加新的实习经历
export async function POST(req: NextRequest) {
  try {
    // 从请求头获取token并验证用户身份
    const authHeader = req.headers.get('authorization');
    
    if (!authHeader || !authHeader.startsWith('Bearer ')) {
      return NextResponse.json(
        { message: '未授权访问' },
        { status: 401 }
      );
    }
    
    const token = authHeader.split(' ')[1];
    
    // 解析JWT token
    let userId;
    try {
      const payload = JSON.parse(atob(token.split('.')[1]));
      userId = payload.id;
      
      if (!userId) {
        return NextResponse.json(
          { message: '无效的用户凭证' },
          { status: 401 }
        );
      }
    } catch (e) {
      console.error('解析token失败:', e);
      return NextResponse.json(
        { message: '无效的用户凭证' },
        { status: 401 }
      );
    }
    
    // 解析请求体
    const body = await req.json();
    
    // 验证必需字段
    if (!body.company || !body.position || !body.period) {
      return NextResponse.json(
        { message: '缺少必要字段' },
        { status: 400 }
      );
    }
    
    // 准备数据
    const internshipData = {
      user_id: userId,
      company: body.company,
      position: body.position,
      period: body.period,
      description: body.description || null
    };
    
    // 添加新的实习经历
    const internshipId = await UserModel.addInternship(internshipData);
    
    // 获取新添加的实习经历详情
    const newInternship = {
      id: internshipId,
      ...internshipData
    };
    
    return NextResponse.json(
      newInternship,
      { status: 201 }
    );
  } catch (error: any) {
    console.error('添加实习经历时出错:', error);
    
    return NextResponse.json(
      { message: '添加实习经历失败', error: error.message },
      { status: 500 }
    );
  }
} 